// pages/home/home.js
var BScroll = require('../../miniprogram_npm/better-scroll/index.js')
var app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        indicatorDots: true,
        vertical: true,
        autoplay: true,
        interval: 3000,
        banner: [],
        duration: 1000,
        background: '',
        category: [],
        nav: 'first',
        navList: [],
        headLine: {},
        list: [],
        page: 1,
        downRefash: true,
        back: true,
        scrollTop: 0,
        showIcon:false
    },
    onLoad: function () {
        this.getBanner(),
            this.getCategory(),
            this.getNavList(),
            this.getHeadLine(),
            this.getList(this.data.page)
    },

    useBetterScroll: function () {
        const query = wx.createSelectorQuery()
        let wrapper = query.select('.content')
        console.log(wrapper)
    },
    bannerChange: function (e) {
        this.setData({
            background: this.data.banner[e.detail.current].bannerColor
        })

    },
    getBanner: function () {
        var that = this;
        wx.request({
            url: 'http://cmsjapi.ffquan.cn/api/category/product/model-detail-by-model-id-new',
            data: {
                entityId: 4,
                modelId: -1,
                proModelId: 1,
                source: 3,
                version: 'v1',
                tuserId: 1317749,
                isWechat: 0,
            },
            header: {
                'content-type': 'application/json' // 默认值
            },
            success(res) {
                that.setData({
                    banner: res.data.data.config,
                    background: res.data.data.config[0].bannerColor
                })

            }
        })
    },
    getCategory: function () {
        var that = this;
        wx.request({
            url: 'http://cmsjapi.ffquan.cn/api/category/product/model-detail-by-model-id-new',
            data: {
                entityId: 4,
                modelId: 20736,
                proModelId: 2,
                source: 3,
                version: 'v1',
                tuserId: 1317749,
                isWechat: 0,
            },
            header: {
                'content-type': 'application/json' // 默认值
            },
            success(res) {
                that.setData({
                    category: res.data.data.config.data
                })
                // console.log(that.data.category)
            }
        })
    },
    changeNav: function (e) {
        var index = e.currentTarget.dataset.num
        this.setData({
            nav: index
        })
        // console.log(this.data.nav)
    },
    getNavList: function () {
        var that = this;
        wx.request({
            url: 'http://cmsjapi.ffquan.cn/api/category/get-category-list',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success(res) {
                that.setData({
                    navList: res.data.data
                })
                // console.log(that.data.navList)
            }
        })
    },
    getHeadLine: function () {
        var that = this;
        wx.request({
            url: 'http://cmsjapi.ffquan.cn/api/category/product/model-detail-by-model-id-new',
            header: {
                'content-type': 'application/json' // 默认值
            },
            data: {
                entityId: 4,
                modelId: 20737,
                proModelId: 13,
                source: 3,
                version: 'v1',
                tuserId: 1317749,
                isWechat: 0,
            },
            success(res) {
                that.setData({
                    headLine: res.data.data
                })
                // console.log(that.data.headLine)
            }
        })
    },
    onButtonClick: function () {
        this.setData({
            page: this.data.page += 1
        })
        this.getList(this.data.page)
    },
    getList(page) {
        var that = this;
        wx.request({
            url: 'http://cmsjapi.ffquan.cn/api/category/index/lingquan-live-new',
            header: {
                'content-type': 'application/json' // 默认值
            },
            data: {
                pageId: page,
                pageSize: 10,
                entityId: 4,
                type: 1,
                version: 'v1',
                tuserId: 1317749,
                isWechat: 0
            },
            success(res) {
                that.setData({
                    list: that.data.list.concat(res.data.data.list)
                })
                console.log(that.data.list)
            }
        })

    },
    scroll(e) {
        if(e.detail.scrollTop>400){
            this.setData({
                showIcon: true
            })
        }else{
            this.setData({
                showIcon: false
            })
        }
    },
    lower(e) {
        this.onButtonClick()
    },
    scrollToTop() {
            this.setData({
                scrollTop: this.data.scrollTop -= 10
            })
    },
    onPullDownRefresh(e) {
        this.getBanner(),
            this.getCategory(),
            this.getNavList(),
            this.getHeadLine(),
            setTimeout(() => {
                this.setData({
                    back: false
                })
            }, 1000)
    }

})